import React from 'react';
import { Modal, Form, Input } from 'antd';
import ArticleCategory from 'components/common/ArticleCategory';
import _ from 'lodash';

const FormItem = Form.Item;

class AddModal extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};
    this.onOK = this.onOK.bind(this);
  }

  onOK() {
    const { onOK, form, onCancel, record, getList } = this.props;
    form.validateFields((err, values) => {
      if (!err) {
        onOK({ ...values, id: record.id }).then(() => {
          onCancel();
          getList();
        });
      }
    });
  }

  render() {
    const { onCancel, form, loading, record = {} } = this.props;
    const { getFieldDecorator } = form;
    const modalOpts = {
      confirmLoading: loading,
      title: '新增类型',
      width: 500,
      visible: true,
      maskClosable: false,
      okText: '确定',
      cancelText: '取消',
      onOk: this.onOK,
      onCancel
    };
    const formItemLayout = {
      labelCol: {
        span: 6
      },
      wrapperCol: {
        span: 18
      }
    };
    const articleProps = {
      val: _.isNil(record.pid) ? '' :record.pid
    };
    return (
      <Modal {...modalOpts}>
        <Form>
          <FormItem
            label="所属父级"
            {...formItemLayout}
          >
            {
              getFieldDecorator('pid', {
                initialValue: record.pid || 0,
                rules: [{ required: true, message: '请选择所属父级' }]
              })(
                <ArticleCategory {...articleProps} />
              )
            }
          </FormItem>
          <FormItem
            label="类型名称"
            {...formItemLayout}
          >
            {
              getFieldDecorator('name', {
                initialValue: record.name || '',
                rules: [{ required: true, message: '请填写类型名称' }]
              })(
                <Input
                  maxLength={30}
                  placeholder="类型名称"
                />
              )
            }
          </FormItem>
        </Form>
      </Modal>
    );
  }
}

export default Form.create()(AddModal);
